package com.example.child_protection.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.child_protection.dao.SystemDiseaseDao;
import com.example.child_protection.dto.info.SystemDiseaseDTO;
import com.example.child_protection.dto.request.SimpleIdRequestDTO;
import com.example.child_protection.dto.request.find.SystemDiseaseRequestDTO;
import com.example.child_protection.dto.response.SystemDiseaseListResponseDTO;
import com.example.child_protection.dto.response.SystemDiseaseResponseDTO;
import com.example.child_protection.enums.ServiceActionEnums;
import com.example.child_protection.model.SystemDiseaseModel;
import com.example.child_protection.service.SystemDiseaseQueryService;
import com.example.child_protection.service.base.MyBaseService;
import com.example.child_protection.service.base.MyServiceExecutor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 科室查询service实现层
 *
 * @author Biz
 * @date 2018-11-2
 */
@Component
public class SystemDiseaseQueryServiceImpl extends MyBaseService implements SystemDiseaseQueryService {

    @Autowired
    public SystemDiseaseDao systemDiseaseDao;

    /**
     * id查询
     *
     * @param requestDTO
     * @return
     */
    public SystemDiseaseResponseDTO findId(SimpleIdRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_DISEASE_FIND_ID, requestDTO, SystemDiseaseResponseDTO.class, new MyServiceExecutor<SimpleIdRequestDTO, SystemDiseaseResponseDTO>() {
            @Override
            protected SystemDiseaseResponseDTO myprocess(SimpleIdRequestDTO requestDTO) throws Exception {
                SystemDiseaseResponseDTO responseDTO = new SystemDiseaseResponseDTO();
                SystemDiseaseModel systemDiseaseModel = systemDiseaseDao.findId(requestDTO.getId());
                SystemDiseaseDTO systemDisease = new SystemDiseaseDTO();
                BeanUtils.copyProperties(systemDiseaseModel, systemDisease);
                responseDTO.setInfo(systemDisease);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }

    /**
     * 列表查询
     *
     * @param requestDTO
     * @return
     */
    public SystemDiseaseListResponseDTO findList(SystemDiseaseRequestDTO requestDTO) {
        return this.service(ServiceActionEnums.SYSTEM_DISEASE_FIND_LIST, requestDTO, SystemDiseaseListResponseDTO.class, new MyServiceExecutor<SystemDiseaseRequestDTO, SystemDiseaseListResponseDTO>() {
            @Override
            protected SystemDiseaseListResponseDTO myprocess(SystemDiseaseRequestDTO requestDTO) throws Exception {
                SystemDiseaseListResponseDTO responseDTO = new SystemDiseaseListResponseDTO();
                SystemDiseaseModel req = new SystemDiseaseModel();
                BeanUtils.copyProperties(requestDTO, req);
                List<SystemDiseaseModel> lists = systemDiseaseDao.findPage(req);
                List<SystemDiseaseDTO> roles = JSONObject.parseArray(JSONArray.toJSONString(lists), SystemDiseaseDTO.class);
                responseDTO.setInfos(roles);
                responseDTO.successfully();
                return responseDTO;
            }
        });
    }
}
